/*
 * @Description: 全局过滤器
 * @Author: 熊伟
 * @LastEditors  : 熊伟
 * @Date: 2019-04-23 11:28:04
 * @LastEditTime : 2019-12-31 16:48:51
 */

import {dateFormat} from '@/utils/tool.js'

/**
 * @description: 获取价格整数部分,并用逗号分隔
 * @param : {Number.String} value
 * @return: {String} 整数
 */
const getPriceInteger = function (value) {
    value = String(value);
    let pointIndex = value.indexOf('.');
    let integer = value;

    // 获取整数部分数字
    if (pointIndex > 0) {
        integer = value.substring(0, pointIndex)
    }

    //将数字用逗号分隔
    let len = integer.length;
    if (len <= 3) {
        return integer;
    }

    let r = len % 3;
    return r > 0 ? integer.slice(0, r) + "," + integer.slice(r, len).match(/\d{3}/g).join(",") : integer.slice(r, len).match(/\d{3}/g).join(",");
}

/**
 * @description: 获取价格小数部分
 * @param : {Number.String} value
 * @return: {String} 小数
 */
const getPriceDecimal = function (value) {
    value = String(value);
    let pointIndex = value.indexOf('.');
    if (pointIndex > 0) {
        return '.' + value.substring(pointIndex + 1)
    } else {
        return '.00'
    }
}

/**
 * @description: 隐藏手机号中间四位
 * @param : {String} mobile
 * @return: {String} 
 */
const hideMobile = function (value) {
    return value && value.replace(/^(\d{3})\d{4}(\d+)/, "$1****$2");
}

// const formatTime = function (value, formatStr) {
//     console.log(value);
//     console.log(formatStr);
//     console.log("------------------------");
//     if (!value && value !== 0) {
//         return ''
//     }
//     if (!isNaN(value)) {
//         return moment(value * 1000).format(formatStr || 'YYYY-MM-DD HH:mm:ss')
//     } else {
//         return moment(value).format(formatStr || 'YYYY-MM-DD HH:mm:ss')
//     }
// }

/**
 * @description: 截取时间字段的日期部分
 * @param : {String} 'YYYY-MM-DD HH:mm:ss'
 * @return: {String} 'YYYY-MM-DD'
 */
const getTimeDate = function (value) {
    if (!value && value !== 0) {
        return ''
    }
    return value.split(' ')[0]
}

const getTimeDateByNum = function (value) {
    if (!value && value !== 0) {
        return ''
    }
    formatTime.call(new Date(value * 1000), "")

}

const formatTime = function (value, formatStr) { //author: meizz   
    if (!value && value !== 0) {
        return ''
    }
    let date = "";
    if (!isNaN(value)) {
        date = new Date(value * 1000);
        return format.call(date, formatStr || 'YYYY-MM-DD HH:mm:ss')
    } else {
        date = new Date(value);

    }
    return format.call(date, formatStr || 'YYYY-MM-DD HH:mm:ss')
}

const format = function (fmt) {    
    var o = {
        "M+": this.getMonth() + 1,                 //月份   
        "D+": this.getDate(),                    //日   
        "H+": this.getHours(),                   //小时   
        "m+": this.getMinutes(),                 //分   
        "s+": this.getSeconds(),                 //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
    };
    if (/(Y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}



const toFixed = function (value, number) {
    let num = Number(value)
    return num ? num.toFixed(number || 2) : '0.00'

}

// 数量转化为 千 量级
const quantityIntoThousands = function (value=0) {
    if (Number(value) >= 1000) {
        let thousandNum = (value / 1000).toFixed(1);
        return thousandNum + "k";
    }
    return value;
}

// 数量转化为 万 量级
const quantityIntoTenThousands = function (value=0) {
    if (Number(value) >= 100000) {
        let thousandNum = (value / 10000).toFixed(2);
        return thousandNum + "万";
    }
    return value;
}

const timeFormate = function(val,format){
    if(val){
        return dateFormat(val,format)
    }else{
        return '';
    }
}

export default {
    getPriceInteger,
    getPriceDecimal,
    hideMobile,
    getTimeDateByNum,
    formatTime,
    getTimeDate,
    toFixed,
    quantityIntoThousands,
    quantityIntoTenThousands,
    timeFormate
}